package zhihui.mobile.presenter.shop;

import android.content.Context;
import android.text.TextUtils;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.app.MyApplication;
import zhihui.mobile.bean.shop.SkuInfo;
import zhihui.mobile.bean.shop.address.ShoppingAddress;
import zhihui.mobile.bean.shop.order.AddOrderInfo;
import zhihui.mobile.bean.shop.order.OrderShopInfo;
import zhihui.mobile.bean.shop.sales.CouponInfo;
import zhihui.mobile.bean.wx.WeixinPayInfo;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.pay.PayManager;
import zhihui.mobile.manager.shop.CouponManager;
import zhihui.mobile.manager.shop.ShopAddressManager;
import zhihui.mobile.manager.shop.ShopManager;
import zhihui.mobile.manager.shop.ShopOrderManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.shop.order.IShopOrderConfirmContract;

/**
 * Created by 张公铭 on 2017/9/1 0001.
 */

public class ShopOrderConfirmPresenter extends BaseShopPresenter implements IShopOrderConfirmContract.IShopOrderConfirmPresenter
{
    private Context mContext;
    private IShopOrderConfirmContract.IShopOrderConfirmView confirmView;
    private AddOrderInfo addOrderInfo = new AddOrderInfo();
    private int pay_money = 0;
    private String orderId = "0";

    public AddOrderInfo getAddOrderInfo()
    {
        return addOrderInfo;
    }

    public int getPay_money()
    {
        return pay_money;
    }

    public void setPay_money(int pay_money)
    {
        this.pay_money -= pay_money;
    }

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        confirmView = (IShopOrderConfirmContract.IShopOrderConfirmView) view;
    }

    @Override
    public void onResume()
    {
        confirmView.refreshAddressDialog();
        confirmView.refreshButtonBuy(PayManager.wx_pay);
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void initData()
    {
        List<SkuInfo> list = new ArrayList<>();
        list.addAll(ShopOrderManager.getInstance().getConfirmList());
        List<OrderShopInfo> orderList = new ArrayList<>();
        int count = 0;
        pay_money = 0;
        for (SkuInfo sku : list)
        {
            if (sku.getSalestock() <= 0)
            {
                continue;
            }
            count += sku.getNum();
            pay_money += (sku.getPrice() * sku.getNum());
            OrderShopInfo orderShopInfo = new OrderShopInfo();
            orderShopInfo.setSkuId(sku.getSkuId());
            orderShopInfo.setSkuNum(sku.getNum());
            orderList.add(orderShopInfo);
        }
        addOrderInfo.setList(orderList);
        confirmView.updateShopList(list);
        confirmView.updateAddress(ShopAddressManager.getInstance().getDefaultAddress());
        confirmView.updateShopInfo(count, (double) pay_money / 100);
    }

    @Override
    public void getAddressList()
    {
        getAddress(confirmView, mContext, new IAddressSuccessListener()
        {
            @Override
            public void success(List<ShoppingAddress> list)
            {
                confirmView.showAddressDialog(list);
            }
        });
    }

    @Override
    public void createOrder()
    {
        if (TextUtils.isEmpty(addOrderInfo.getCityCode()))
        {
            confirmView.showMessage("请选择收货地址");
            return;
        }
        if (!TextUtils.isEmpty(orderId) && !orderId.equals("0"))
        {
            confirmView.showPayDialog();
            return;
        }

        confirmView.showLoading();
        ShopOrderManager.getInstance().addShopOrder(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                confirmView.hindLoading();
                try
                {
                    orderId = result.getString("data");
                    if (orderId.equals("0"))
                    {
                        confirmView.showGoShopCartDialog();
                        return;
                    }
                    confirmView.showPayDialog();
                    ShopManager.getInstance().refreCartShopList(addOrderInfo);
                } catch (JSONException e)
                {
                    confirmView.showMessage(mContext.getString(R.string.json_error));
                    e.printStackTrace();
                }
            }

            @Override
            public void failed(String message)
            {
                confirmView.hindLoading();
                confirmView.showMessage(message);
            }
        }, addOrderInfo);
    }

    @Override
    public void updateCurAddress(ShoppingAddress address)
    {
        ShopAddressManager.getInstance().setCurAddress(address);
    }

    @Override
    public void getSalesList()
    {
        confirmView.showLoading();
        StringBuffer sb = new StringBuffer();
        int money = 0;
        for (int i = 0; i < ShopManager.getInstance().getSelectSku().size(); i++)
        {
            sb.append(ShopManager.getInstance().getSelectSku().get(i).getSkuId());
            money += ShopManager.getInstance().getSelectSku().get(i).getPrice() * ShopManager.getInstance().getSelectSku().get(i).getNum();
            if (i < (ShopManager.getInstance().getSelectSku().size() - 1))
            {
                sb.append(",");
            }
        }

        CouponManager.getInstance().getUseCouponList(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                confirmView.hindLoading();
                Gson gson = new Gson();
                try
                {
                    List<CouponInfo> list = gson.fromJson(result.getJSONArray("data").toString(), new TypeToken<List<CouponInfo>>()
                    {
                    }.getType());
                    if (list != null && list.size() > 0)
                    {
                        confirmView.showCouponDialog(list);
                    } else
                    {
                        confirmView.showMessage("没有可用优惠券");
                    }
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    confirmView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                confirmView.hindLoading();
                confirmView.showMessage(message);
            }
        }, sb.toString(), money, 5101);
    }

    @Override
    public void ilpay()
    {
        if (orderId.equals("0") || pay_money <= 0)
        {
            confirmView.showMessage("支付信息错误！");
            return;
        }

        confirmView.showLoading();
        PayManager.getInstance().ilpay(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                confirmView.hindLoading();
                try
                {
                    String data = result.getString("data");
                    confirmView.ilPay(data);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    confirmView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                confirmView.hindLoading();
                confirmView.showMessage(message);
            }
        }, "" + orderId, pay_money);
    }

    @Override
    public void weixinPay()
    {
        if (orderId.equals("0") || pay_money <= 0)
        {
            confirmView.showMessage("支付信息错误！");
            return;
        }

        confirmView.showLoading();
        PayManager.getInstance().weixin(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                confirmView.hindLoading();
                try
                {
                    Gson gson = new Gson();
                    WeixinPayInfo payInfo = gson.fromJson(result.getJSONObject("data").toString(), WeixinPayInfo.class);
                    confirmView.weixinPay(payInfo);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    confirmView.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                confirmView.hindLoading();
                confirmView.showMessage(message);
            }
        }, "" + orderId, pay_money);
    }

    @Override
    public void goOrderDetail()
    {
        super.goShopOrderDetail(mContext, orderId, confirmView);
        MyApplication.finishActivity(mContext.getClass());
    }
}
